<template>
	<div class="log">
		<el-button type="success" icon="el-icon-folder-opened" @click="openScanner">打开扫描仪</el-button>
		<el-button type="success" icon="el-icon-camera" @click="doScanAndUpload">扫描</el-button>
		<a href="" id="proxyApp" name="proxyApp"></a>
	</div>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'

	export default {
		name: 'Log',
		data() {
			return {
				exeUrl: 'http://127.0.0.1:55055',
				exeHdr: {
					timeout: 600000,
					'headers': {
						'Content-Type': 'application/x-www-form-urlencoded'
					}
				},
				currDirKey: "" //当前文件目录key
			}
		},
		computed: {
			...mapGetters(['permissions']),
		},
		methods: {
			openScanner() {
				var refObj = document.getElementById("proxyApp");
				refObj.href = "CfssAsag://minimize";
				refObj.click();
			},
			doScanAndUpload(command, imgid, scanFlag) {
				command = "appendimg";
				imgid = "";
				scanFlag = 0;
				var proxCmd = {};
				if (scanFlag == 0) {
					proxCmd.command = "scan0"; // 默认扫描
				} else if (scanFlag == 1) {
					proxCmd.command = "scan1"; // 批量扫描
				} else
					proxCmd.command = "scan2"; // 自定义扫描
				proxCmd.imagetype = "";
				proxCmd.imagedata = "";
				try {
					this.$http.post(this.exeUrl, proxCmd, this.exeHdr).then(response => {
						if (response.data.resp_code === "5500") { // 取消扫描
							this.scanStat = 9;
						} else if (response.data.resp_code != "0000") {
							this.scanStat = 9;
							if (scanFlag != 1) {
								this.$notify({
									title: "扫描失败",
									message: response.data.resp_msg,
									type: "warning",
									duration: 2000
								});
							}
						} else {
							for (var zz in response.data.imagedata) {

								var imgDat = response.data.imagedata[zz].substring(22); //response.data.imagedata.substring(22)
								this.currImg = response.data.imagedata;
								this.base64Img = imgDat;

								let fileFormData = new FormData();
								fileFormData.append("command", command);
								fileFormData.append("pathkey", this.currDirKey);
								fileFormData.append("filename", response.data.imagefile);
								fileFormData.append("imagedata", imgDat);
								fileFormData.append("keyid", imgid);
								fileFormData.append("bakflg", command == "change" ? "0" : "1");

								let requestConfig = {
									headers: {
										'Content-Type': 'multipart/form-data'
									}
								};
								//这里进行业务的相关处理（上传文件或者图片的对应的处理等）
							} // End For
						} // End Else
					}); // End Scan
				} catch (err) {
					this.$notify({
						title: "扫描失败",
						message: "扫描失败,请检查扫描代理是否启动",
						type: "warning",
						duration: 2000
					});
				}
			}
		}
	}
</script>

<style>
</style>
